/*
 * Copyright 2019-present HiveMQ GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hivemq.extensions.rbac.redis.configuration;

import com.hivemq.extensions.rbac.redis.configuration.entities.ExtensionRedisConfig;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;

import static com.hivemq.extensions.rbac.redis.ExtensionConstants.EXTENSION_CONFIG_LEGACY_LOCATION;
import static com.hivemq.extensions.rbac.redis.ExtensionConstants.EXTENSION_CONFIG_LOCATION;

/**
 * xml配置工具
 */
public class ExtensionConfiguration {

    private static final @NotNull Logger LOG = LoggerFactory.getLogger(ExtensionConfiguration.class);
    private final @NotNull XmlParser xmlParser = new XmlParser();
    private final @NotNull ExtensionRedisConfig extensionRedisConfig;

    public ExtensionConfiguration(final @NotNull File extensionHomeFolder) {
        final ConfigResolver configResolver = new ConfigResolver(extensionHomeFolder.toPath(),
                EXTENSION_CONFIG_LOCATION,
                EXTENSION_CONFIG_LEGACY_LOCATION);
        extensionRedisConfig = read(configResolver.get().toFile());
    }

    public @NotNull ExtensionRedisConfig getExtensionConfig() {
        return extensionRedisConfig;
    }

    /**
     * @param file the new config file to read.
     * @return the new config based on the file contents or null if the config is invalid
     */
    private @NotNull ExtensionRedisConfig read(final @NotNull File file) {
        final ExtensionRedisConfig defaultConfig = new ExtensionRedisConfig();
        if (!file.exists()) {
            LOG.warn("File auth extension configuration file {} missing, using defaults", file.getAbsolutePath());
            return defaultConfig;
        }
        if (!file.canRead()) {
            LOG.warn("Unable to read file auth extension configuration file {}, using defaults",
                    file.getAbsolutePath());
            return defaultConfig;
        }

        try {
            final ExtensionRedisConfig newExtensionRedisConfig = xmlParser.unmarshalExtensionConfig(file);
            if (newExtensionRedisConfig.getReloadInterval() < 1) {
                LOG.warn(
                        "Credentials reload interval for file auth extension must be greater than 0, using default interval " +
                                defaultConfig.getReloadInterval());
                newExtensionRedisConfig.setReloadInterval(defaultConfig.getReloadInterval());
            }
            if (newExtensionRedisConfig.getRedisHost().isBlank()) {
                LOG.warn("Unknown RedisHost:" +
                        defaultConfig.getRedisHost());
            }
            if (newExtensionRedisConfig.getRedisPort() < 0) {
                LOG.warn("Unknown redisPort:" +
                        defaultConfig.getRedisHost());
            }
            if (newExtensionRedisConfig.getRedisPassword().isBlank()) {
                LOG.warn("Unknown redisPassword:" +
                        defaultConfig.getRedisPassword());
            }
            LOG.info("RedisConfig：{}", newExtensionRedisConfig.toString());
            return newExtensionRedisConfig;
        } catch (final IOException e) {
            LOG.warn("Could not read file auth extension configuration file, reason: {}, using defaults",
                    e.getMessage());
            return defaultConfig;
        }
    }
}
